/**
 * @author user
 */
$(function(){
	var _iptBtn = $('#j-input');
	var _ipt	= $('#j-inputArea');
	var _rec    = $('#j-rec');
	var _conIpt = $('#j-conIpt');
	var _conBtn = $('#j-conBtn');
	var _toIpt  = $('#j-to');
	var _toArea = $("#j-toArea");
	var _toLst = $('#j-toLst');
	var _personInfo = $('#j-personInfo');
	var _connectArea = $("#j-connect");
	var _disablelayer = $("#j-disablelayer");
	var _sendArea  = $("#j-sendArea");
	var _isGroup = $('#j-group');
	_conIpt.val('');
	_personInfo.hide();
	_connectArea.show();
	_sendArea.hide();
	_rec.hide();
	_toArea.hide();
	_disablelayer.show();
	
	var _usr = _conIpt.val();
	var _ws = '';
	var _getConnection = function(){
		_ws = new WebSocket("ws://192.168.144.161:8080/sendMsg");
		/* websocket 接收到消息的回调 **/
		_ws.onmessage = function(evt){
				var _res = JSON.parse(evt.data);
				if(_res.type==0){
					//登录成功后接收到消息 
					_connectArea.hide();
					_disablelayer.hide();
					_personInfo.fadeIn('slow');
					_sendArea.fadeIn('slow');
					_toArea.fadeIn('slow');
					_rec.fadeIn('slow');
				}else if(_res.type==1&&_res.content){
					//普通消息
					var _tmpHtml = _rec.html();
					_tmpHtml += "From:   "+_res.from+"<br/>"+_res.content+"<br/>";
					_rec.html(_tmpHtml);
					_ipt.val('');
				}else if(_res.type==2){
					//发给自己的消息
					alert('悄悄话说给自己听哈哈');
					return;
				}else if(!_res.type){
					//有人上线的通知消息
					var _friends = _res;
					var _tmpHtml1 = _toIpt.html();
					_toLst.html('');
					for(var _key in _res){					
						//_tmpHtml1 += '<li>'+_res.connectionLst[key]+'</li>';
						var _li = $("<li/>").text(_res[_key]);
						_li.bind('click',(function(_li){
							return	function(){
							_toIpt.val(_li.text());
						}})(_li));
						_toLst.append(_li);			
					}
				}else if(_res.type==-2){
					//输入的用户已登录的返回消息
					alert("该用户已登录，换一个呗");
					return;
				}else if(_res.type==10){
					//群发消息
					var _tmpHtml = _rec.html();
					_tmpHtml += "群发消息 From:   "+_res.from+"<br/>"+_res.content+"<br/>";
					_rec.html(_tmpHtml);
					_ipt.val('');
					if(_toIpt.is(":disabled")){
						_toIpt.removeAttr("disabled");
					}
					if(_isGroup.is(':checked')){
						_isGroup.removeAttr('checked');
					}
				}
			}
		_ws.onclose = function() {
			   _ws = new WebSocket("ws://192.168.144.161:8080/sendMsg");
	                    };   
	      
	    _ws.onopen = function() {
			_usr = _conIpt.val();
			var _openMsgVo = {type:0,
							  from:_usr,
							  to:1,
							  content:1}
			_ws.send(JSON.stringify(_openMsgVo));
			_conIpt.val("连接中····");
	                    }; 
	}
	
	/*
	 * 群发消息 
	 */
	var _sendGroupMsg = function(){
		var _messageVo = {type:10,
						  from:_usr,
						  content:_ipt.val()}
						  
			var _tmpHtml = _rec.html();
			_tmpHtml += "群发消息 :   <br/>"+_messageVo.content+"<br/>";
			_rec.html(_tmpHtml);	  
       		 _ws.send(JSON.stringify(_messageVo));
	}
	/*
	 * 发送消息
	 */
	var _sendMsg = function(){
		if(_isGroup.is(':checked')){			
			_sendGroupMsg();
			return;
		}
		if(_toIpt.val()==''){
			alert('请选择一位联系人');
			return;
		}	
		var _messageVo = {type:1,
						  from:_usr,
						  to:_toIpt.val(),
						  content:_ipt.val()}
						  
			var _tmpHtml = _rec.html();
			_tmpHtml += "To:   "+_messageVo.to+"<br/>"+_messageVo.content+"<br/>";
			_rec.html(_tmpHtml);	  
        _ws.send(JSON.stringify(_messageVo));
	};
	/*
	 * 建立连接
	 */
	var _connect = function(){		
		if(_ws==''){
			_getConnection();
		}
				
	}
	
	/**
	 * 选择群发或者取消群发 
	 */
	var _onGourpChecked = function(){
		_toIpt.val("");
		if(_toIpt.is(':disabled')){
			_toIpt.removeAttr('disabled');
		}else{
			_toIpt.attr("disabled","disabled");			
		}
		
	}
	_iptBtn.bind("click",_sendMsg); 
	_conBtn.bind("click",_connect);
	_isGroup.bind('click',_onGourpChecked);
});
